package com.pinyougou.manager.controller;


import com.github.pagehelper.PageInfo;
import com.pinyougou.Result;
import com.pinyougou.pojo.TbBrand;
import com.pinyougou.sellergoods.service.BrandService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/brand")
public class BrandController {

    @Autowired
    private BrandService brandService;

    @RequestMapping("/delete")
    public Result  deleteBrand(@RequestBody List<Long> ids) {
        try {
            int num = brandService.delete(ids);
            if (num > 0) {
                return new Result(true, "删除成功");
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return new Result(true, "删除失败");
    }

    /**
     * 根据Id查询
     *
     * @param id
     * @return
     */
    @RequestMapping("/{id}")
    public TbBrand findById(@PathVariable(value = "id") Long id) {
        return brandService.getById(id);
    }

    /**
     * 修改品牌
     *
     * @param brand
     * @return
     */
    @RequestMapping("/update")
    public Result updateBrand(@RequestBody TbBrand brand) {
        try {
            int num = brandService.updateBrand(brand);
            if (num > 0) {
                //修改成功
                return new Result(true, "修改成功");
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return new Result(false, "修改失败");
    }

    /**
     * 保存品牌
     *
     * @param Brand
     * @return
     */
    @RequestMapping("/save")
    public Result save(@RequestBody TbBrand Brand) {
        try {
            int num = brandService.add(Brand);
            if (num > 0) {
                //保存成功
                return new Result(true, "保存成功");
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return new Result(false, "保存失败");
    }


    /**
     * 展示全部品牌
     *
     * @param page
     * @param size
     * @return
     */
    @RequestMapping("/list")
    public PageInfo<TbBrand> findAll(@RequestParam(value = "page", required = true, defaultValue = "1") Integer page,
                                     @RequestParam(value = "size", required = true, defaultValue = "5") Integer size,
                                     @RequestBody TbBrand brand) {
        PageInfo<TbBrand> all = brandService.findAll(page, size, brand);
        return all;
    }
}
